<?php
class RobotsController extends  IndexController {

    public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->RobotsModel    = new RobotsModel();
        $this->UserLogsModel  = new LogsModel();
        $this->SourceModel    = new SourceModel();
        $this->AddressModel   = new AddressModel();
        $this->ArticleModel   = new ArticleModel();
        $this->Session        = Yaf_Registry::get("session");
    }
	
    public function listAction()
    {
        $typicle_info      = $this->ArticleModel->articleType();
        $coll_site_info    = $this->SourceModel->getCollWebsiteinfo();
        $address_info      = $this->AddressModel->getAddressInfo();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $robots_info       = $this->RobotsModel->selectRobots($startLimit,$offset);
        $allCounts         = $this->RobotsModel->getAllCount();
        $allpage           = $this->utl::page('/Robots/list', $page, $allCounts[0]['rows'], $offset, $status='');

        $robots_list       = array();
        foreach ($robots_info AS $k => $v)
        {
            $robots_list[$k]                 = $v;
            $robots_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $robots_list[$k]['rows']         = $allCounts[0]['rows'];
            $robots_list[$k]['every_page']   = $offset;
            foreach ($coll_site_info  AS $ks => $vs)
            {
                if ($v['website_id'] === $vs['website_id'])
                {
                    $robots_list[$k]['website_name'] = $vs['web'];break;
                }
            } 
            foreach ($typicle_info as $ks => $vs) 
            {
                if($v['article_type_id'] === $vs['article_type_id'])
                {
                    $robots_list[$k]['article_name'] = $vs['name'];
                    $robots_list[$k]['article_remark'] = $vs['remark'];
                    break;
                }
            }
        }
        
        foreach ($robots_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $robots_list[$key]['robots_status'] = '正常';
            }
            else if($value['status'] == 0)
            {
                $robots_list[$key]['robots_status'] ='无效';
            }
        }
        $this->getView()->assign("typicle_list",$typicle_info);
        $this->getView()->assign("robots_list",$robots_list);
        $this->getView()->assign("address_list",$address_info);
        $this->getView()->assign("allpage",$allpage);
    }

    public function pageAction()
    {
        $typicle_info      = $this->ArticleModel->articleType();
        $coll_site_info    = $this->SourceModel->getCollWebsiteinfo();
        $address_info      = $this->AddressModel->getAddressInfo();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $robots_info       = $this->RobotsModel->selectRobots($startLimit,$offset);
        $allCounts         = $this->RobotsModel->getAllCount();
        $allpage           = $this->utl::page('/Robots/list', $page, $allCounts[0]['rows'], $offset, $status='');

        $robots_list       = array();
        foreach ($robots_info AS $k => $v)
        {
            $robots_list[$k]                 = $v;
            $robots_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $robots_list[$k]['rows']         = $allCounts[0]['rows'];
            $robots_list[$k]['every_page']   = $offset;
            foreach ($coll_site_info  AS $ks => $vs)
            {
                if ($v['website_id'] === $vs['website_id'])
                {
                    $robots_list[$k]['website_name'] = $vs['web'];break;
                }
            } 
            foreach ($typicle_info as $ks => $vs) 
            {
                if($v['article_type_id'] === $vs['article_type_id'])
                {
                    $robots_list[$k]['article_name'] = $vs['name'];
                    $robots_list[$k]['article_remark'] = $vs['remark'];
                    break;
                }
            }
        }
        foreach ($robots_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $robots_list[$key]['robots_status'] = '正常';
            }
            else if($value['status'] == 0)
            {
                $robots_list[$key]['robots_status'] ='无效';
            }
        }
        $this->getView()->assign("robots_list",$robots_list);
        $this->getView()->assign("address_list",$address_info);
        exit(json_encode($robots_list, true));
    }

    public function currentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $robots_info       = $this->RobotsModel->selectRobots($startLimit,$offset);
        $allCounts         = $this->RobotsModel->getAllCount();
        $allpage=$this->utl::page('/Robots/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Robots', 'list');
            return;
        }
        $rs = $this->RobotsModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "robots";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }

    public function addAction()
    {
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->RobotsModel->add($post);
            $session_add = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "robots";
                $session_add[$k]['action'] = "add";
            }
            $this->UserLogsModel->addUser($session_add);
            if (false === $rs)
            {
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }

            exit('{"status":true,"info":"添加成功！"}');


        }
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Robots', 'list');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->RobotsModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "robots";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs)
            {
                exit('{"status":false,"info":"编辑失败！"}');
                return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }
}
?>